var win,fp;
var initData = function(){
    
    Ext.get('index').on('click',function(){
         Ext.get('main').dom.src = 'main!index.action';
    });
    Ext.get('chgpwd').on('click',function(){
    	fp.form.reset();
    	win.setTitle('修改密码');
        win.show();
    });
    Ext.get('logout').on('click',function(){
        window.location = 'logon!logout.action';
    });
};
var buildWin = function(){
    fp = new Ext.form.FormPanel({
        labelWidth:90,
        labelAlign:'right',
        bodyStyle:'padding:10px',
        items:[{
            layout:'form',
            border:false,
            defaults:{border:false,width:180},
            items:[{
                    fieldLabel:'密码',
                    name:'passwd',
                    xtype:'textfield',
                    inputType: 'password',
                    allowBlank:false
                    
                },{
                    fieldLabel:'重复密码',
                    name:'repassword',
                    xtype:'textfield',
                    inputType: 'password',
                    allowBlank:false,
                    vtype:'password',
                    confirmTo:'passwd'
            	}]
        	}]
    });
    
    win = new Ext.Window({
        el:'win',
        width:350,
        height:185,
        closeAction:'hide',
        layout:'fit',
        modal:true,
        items:fp,
        buttons:[{
            text:'保存',
            handler:savePwd
        },{
            text:'取消',
            handler:function(){win.hide();}
        }]
    });
    
};
var buildLayout = function(){
	
    var layout = new Ext.Viewport({
        layout:'border',
        items:[{
            region:'north',
            height:55,
            contentEl:'banner'
        },{
            title:'导航菜单',
            region:'west',
            width:180,
            split:true,
            collapsible:true,
            layout:'accordion',
            defaults:{autoScroll:true,border:false},
            layoutConfig:{
                animate:true
            }
        },{
            region:'center',
            contentEl:'main-div'
        }]
    });

    new Ext.data.Store({
        proxy:new Ext.data.HttpProxy({url:'index!getMainMenus.action'}),
        reader:new Ext.data.JsonReader({},['id','title']),
        autoLoad:true
    }).on('load',function(store,records){
        var nav = layout.items.get(1);
        for (var i=0; i<records.length; i++){
        	
            var menu = new Ext.Panel({
                title:records[i].data.title,
                iconCls:'nav'
            });
           
            var root = new Ext.tree.AsyncTreeNode({
                text:records[i].data.title,
                id:records[i].data.id
            });
            
            var treeLoader  = new Ext.tree.TreeLoader({dataUrl:'index!getTreeMenus.action'});
            treeLoader.on("beforeload",function(treeLoader,node){
            	treeLoader.baseParams.moduleId = node.id;  
            });  
            
            var tid = 'Tree'+records[i].data.id;
            var tree = new Ext.tree.TreePanel({
            	id:tid,
                loader:treeLoader,
                root:root,
                border:false,
                rootVisible:false
            });
            tree.on('click',function(node){
                if (node.isLeaf()){
                    Ext.get('main').dom.src = node.attributes.funcUrl;
                }
            });
            tree.on('beforeload',function(node){
            	
    		});
            
            menu.add(tree);
            nav.add(menu);
        }
        layout.doLayout();
    });
};

var init = function(){
    initData();
    buildWin();
    buildLayout();
};

Ext.onReady(init);

var savePwd = function(){
	if (fp.form.isValid() == false){
        return;
    }

    fp.form.submit({
        url:'user!savePwd.action',

        success:function(){
            win.hide();
            Ext.MessageBox.alert('提示','密码修改成功!');
        },
        failure:function(form,action){
            Ext.MessageBox.alert('警告','密码修改失败!');
        }
    })

};